package com.dimenlink.component;

import com.dimenlink.entity.config.AppConfig;
import com.dimenlink.entity.constants.Constants;
import com.dimenlink.entity.dto.SysSettingDto;
import com.dimenlink.entity.dto.TokenUserInfoDto;
import com.dimenlink.entity.dto.UploadingFileDto;
import com.dimenlink.entity.enums.DateTimePatternEnum;
import com.dimenlink.entity.po.CategoryInfo;
import com.dimenlink.entity.po.VideoInfoFilePost;
import com.dimenlink.redis.RedisUtils;
import com.dimenlink.utils.DateUtil;
import com.dimenlink.utils.StringTools;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Component
public class RedisComponent {
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private AppConfig appConfig;

    public String saveCheckCode(String checkCode) {
        String checkCodeKey = UUID.randomUUID().toString();
        redisUtils.setex(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey,
                checkCode, Constants.REDIS_KEY_EXPIRE_ONE_MINUTE*10);
        return checkCodeKey;
    }

    public String getCheckCode(String checkCodeKey) {
        return (String) redisUtils.get(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
    }

    public void cleanCheckCode(String checkCodeKey) {
        redisUtils.delete(Constants.REDIS_KEY_CHECK_CODE + checkCodeKey);
    }

    public void saveTokenInfo(TokenUserInfoDto tokenUserInfoDto){
        //TODO 完善出唯一ID
        String token = UUID.randomUUID().toString();
        tokenUserInfoDto.setExpireAt(System.currentTimeMillis() + Constants.REDIS_KEY_EXPIRE_ONE_DAY*7);
        tokenUserInfoDto.setToken(token);
        redisUtils.setex(Constants.REDIS_KEY_TOKEN_WEB + token,
                tokenUserInfoDto, Constants.REDIS_KEY_EXPIRE_ONE_DAY*7);
    }
    public TokenUserInfoDto getTokenInfo(String token){
        return (TokenUserInfoDto) redisUtils.get(Constants.REDIS_KEY_TOKEN_WEB + token);
    }

    public String getTokenInfo4Admin(String token){
        return (String) redisUtils.get(Constants.REDIS_KEY_TOKEN_ADMIN + token);
    }

    public void cleanToken(String  token){
        redisUtils.delete(Constants.REDIS_KEY_TOKEN_WEB + token);
    }


    public String saveTokenInfo4Admin(String account) {
        String token = UUID.randomUUID().toString();
        redisUtils.setex(Constants.REDIS_KEY_TOKEN_ADMIN + token,
                account, Constants.REDIS_KEY_EXPIRE_ONE_DAY);
        return token;
    }

    public void cleanToken4Admin(String  token){
        redisUtils.delete(Constants.REDIS_KEY_TOKEN_ADMIN + token);
    }

    public void saveCategoryList(List<CategoryInfo> categoryInfoList) {
        redisUtils.set(Constants.REDIS_KEY_CATEGORY_LIST, categoryInfoList);
    }

    public List<CategoryInfo> getCategoryList() {
        return (List<CategoryInfo>) redisUtils.get(Constants.REDIS_KEY_CATEGORY_LIST);
    }

    public String savePreVideoFileInfo(String userId, String fileName, Integer  chunks) {
        String uploadId = StringTools.getRandomString(Constants.LENGTH_15);
        UploadingFileDto fileDto = new UploadingFileDto();
        fileDto.setChunks(chunks);
        fileDto.setFileName(fileName);
        fileDto.setUploadId(uploadId);
        fileDto.setChunkIndex(0);
        String day = DateUtil.format(new Date(), DateTimePatternEnum.YYYY_MM_DD.getPattern());
        String foldPath = day + "/" + userId + uploadId;
        String folder = appConfig.getProjectFolder() + Constants.FILE_FOLDER  + Constants.FILE_FOLDER_TEMP + foldPath;
        File folderFile = new File(folder);
        if(!folderFile.exists()){
            folderFile.mkdirs();
        }
        fileDto.setFilePath(foldPath);
        redisUtils.setex(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId,
                fileDto, Constants.REDIS_KEY_EXPIRE_ONE_DAY);
        return uploadId;

    }

    public UploadingFileDto getUploadVideoFile(String userId, String uploadId) {
        return (UploadingFileDto) redisUtils.get(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);
    }

    public SysSettingDto getSysSettingDto() {
        SysSettingDto sysSettingDto = (SysSettingDto) redisUtils.get(Constants.REDIS_KEY_SYS_SETTING);
        if(sysSettingDto == null){
            sysSettingDto = new SysSettingDto();
        }
        return sysSettingDto;
    }

    public void updatePreVideoFileInfo(String userId, UploadingFileDto fileDto) {
        redisUtils.setex(Constants.REDIS_KEY_UPLOADING_FILE + userId + fileDto.getUploadId(),
                fileDto, Constants.REDIS_KEY_EXPIRE_ONE_DAY);
    }

    public void delVideoFileInfo(String userId, String uploadId){
        redisUtils.delete(Constants.REDIS_KEY_UPLOADING_FILE + userId + uploadId);
    }

    /**
     * 添加文件删除任务
     * @param videoId
     * @param delFilePathList
     */
    public void addFile2DelQueue(String videoId, List<String> delFilePathList) {

        redisUtils.lpushAll(Constants.REDIS_KEY_FILE_DEL + videoId, delFilePathList, Constants.REDIS_KEY_EXPIRE_ONE_DAY*7);
    }

    /**
     * 获取文件删除任务
     * @param videoId
     * @return
     */
    public List<String> getDelFileList(String videoId) {
        return redisUtils.getQueueList(Constants.REDIS_KEY_FILE_DEL + videoId);
    }

    public void cleanDelFileList(String videoId) {
        redisUtils.delete(Constants.REDIS_KEY_FILE_DEL + videoId);
    }

    /**
     * 添加文件转码任务
     * @param addFileList
     */
    public void addFile2TransferQueue(List<VideoInfoFilePost> addFileList) {
        redisUtils.lpushAll(Constants.REDIS_KEY_FILE_TRANSFER, addFileList, 0L);
    }

    /**
     * 添加单个文件转码任务
     * @param videoInfoFilePost
     */
    public void addFile2TransferQueue4Single(VideoInfoFilePost videoInfoFilePost) {
        redisUtils.lpush(Constants.REDIS_KEY_FILE_TRANSFER, videoInfoFilePost, 0L);
    }

    /**
     * 获取文件转码任务
     * @return
     */
    public VideoInfoFilePost getFileFromTransferQueue() {
        return (VideoInfoFilePost) redisUtils.rpop(Constants.REDIS_KEY_FILE_TRANSFER);
    }
}
